package common

import (
	"time"

	logger "multitude-cloud-exporter/pkg/util/log"

	"github.com/prometheus/client_golang/prometheus"
)

//TODO 每种资源都需实现该接口
type Resource interface {
	UpdateMetrics()
	GetRegistry() *prometheus.Registry
}

type BaseResource struct {
	//TODO 需根据调用情况，调整
	Name       string               //资源名称
	Type       string               //类型 mysql、mongodb
	Cloud      string               //云商
	CloudID    string               //云商ID
	CloudType  string               //云商类型 RDS、DDS
	UpdateTime time.Time            //最近更新时间
	Others     map[string]string    //其他调用接口所需信息及参数
	Registry   *prometheus.Registry //metrics表
	Labels     map[string]string    //labels
	Metrics    map[string]Metric
}

func (r *BaseResource) Describe(ch chan<- *prometheus.Desc) {
	for _, m := range r.Metrics {
		ch <- m.Get().Desc
	}
}

func (r *BaseResource) Collect(ch chan<- prometheus.Metric) {
	for _, m := range r.Metrics {
		ch <- prometheus.NewMetricWithTimestamp(m.Get().Time,
			prometheus.MustNewConstMetric(m.Get().Desc,
				prometheus.GaugeValue, m.Get().Value))
	}
}

func (r *BaseResource) GetRegistry() *prometheus.Registry {
	if nil == r.Registry {
		logger.Error("registry is not ready.")
	}
	return r.Registry
}
